﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Xml.Serialization;
using cndp.Model.IModel;

namespace cndp.Model
{
    
    ///<summary>
    /// ContestList - ContestList används som en lista över alla Contest, den kan t.ex. ta bort, redigera, eller lägga till en tävling. Den ärver från BindingList.
    ///</summary>
    [Serializable]
    public class ContestList : BindingList<Contest>, IContestList
    {
        #region Methods

        public void DeleteContestByIndex(int index)
        {
            if (Count != 0)
            {
                RemoveAt(index);
            }
        }

        public bool EditContest(Contest e)
        {
            foreach (var item in this)
            {
                if (item.Id == e.Id)
                {
                    if (e.Location != null)
                    {
                        item.Location = e.Location;
                    }
                    if (e.StartTime != null)
                    {
                        item.StartTime = e.StartTime;
                    }
                    if (e.FinishTime != null)
                    {
                        item.FinishTime = e.FinishTime;
                    }
                    if (e.DeadlineDate != null)
                    {
                        item.DeadlineDate = e.DeadlineDate;
                    }
                    if (e.ContestAdministrator != null)
                    {
                        item.ContestAdministrator = e.ContestAdministrator;
                    }
                    return true;
                }
            }
            return false;
        }

        public bool AddContest(Contest e)
        {
            this.Add(e);
            return true;
        }

        public int GetNumberOfDiversForSpecificContest(int index)
        {
            return this[index].DiverRegister.Count;
        }

        public int GetNumberOfJudgesForSpecificContest(int index)
        {
            return this[index].JudgeRegister.Count;
        }

        public Contest GetContest(int index)
        {
            return this[index];
        }

        public ContestList GetContestList()
        {
            return this;
        }

        public void SaveContestListToXmlFile(string fileName)
        {
            Stream stream = null;
            try
            {
                stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.Write);
                var xs = new XmlSerializer(typeof(ContestList));
                xs.Serialize(stream, this);
            }
            catch (Exception)
            {
                throw new Exception("Contest::SaveContestListToXmlFile - Exception while writing to file");
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
        }

        public void ReadContestListFromXmlFile(string fileName)
        {
            Stream stream = null;
            try
            {
                stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                var xs = new XmlSerializer(typeof(ContestList));
                var tempContestList = (ContestList)xs.Deserialize(stream);
                foreach (var tempContest in tempContestList)
                {
                    Add(tempContest);
                }
            }
            catch (Exception)
            {
                throw new Exception("Contest::ReadContestListToXmlFile - Exception while reading from file");
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();             
                }
            }
        }

        public Contest ReadContestFromXmlFile(string fileName)
        {
            Stream stream = null;
            try
            {
                stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                var xs = new XmlSerializer(typeof(Contest));
                var tempContest = (Contest)xs.Deserialize(stream);
                return tempContest;
            }
            catch (Exception)
            {
                throw new Exception("Contest::ReadContestFromFile - Exception while reading from file");
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }



        }

        #endregion
    }
}